﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UserInterface
{
    public partial class Edit : Form
    {
        Crossing[] cr;
        int[] index = new int[7];
        int crIndx = -1;
        bool save =false;
        List<Lane> allLanes = new List<Lane>();
        List<PedLane> pedLanes = new List<PedLane>();
        LinkedList<TrafficLight>[] trafficLights = new LinkedList<TrafficLight>[4];


        public Edit(Map mp, char tab)
        {
            InitializeComponent();
            cr = mp.GetAllCrossings();
            for (int i = 0, j = 0; i < 7; i++)
            {
                if (mp.GetAllCrossings()[i] != null)
                {
                    this.cmbx_crossing.Items.Add(i + " " + cr[i].GetType());
                    index[j] = i;
                    j++;
                }
            }
            if (tab == 'l')
                this.tab_Properties.SelectedTab = tab_Properties.TabPages[0];
            else if (tab == 'c')
                this.tab_Properties.SelectedTab = tab_Properties.TabPages[1];
            else
                this.tab_Properties.SelectedTab = tab_Properties.TabPages[2];
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            crIndx = index[this.cmbx_crossing.SelectedIndex];
            Label tmp;

            for (int i = 1; i < 7; i++)
            {
                tmp = (Label)this.grpb_gripPosition.Controls[this.grpb_gripPosition.Controls.IndexOfKey(("lbl_" + i))];
                tmp.BackColor = Color.Silver;
            }
            tmp = (Label)this.grpb_gripPosition.Controls[this.grpb_gripPosition.Controls.IndexOfKey(("lbl_" + crIndx))];
            tmp.BackColor = Color.Green;

            if (cr[crIndx].GetType() == typeof(CrossingType1))
            {
                this.lbl_type.Text = "Type: 1";
                this.pbox_imgType.Image = Rsrc_Images.Type1_LaneNaming;
            }
            else
            {
                this.lbl_type.Text = "Type: 2";
                this.pbox_imgType.Image = Rsrc_Images.Type2_LaneNaming;
            }


            if (this.tab_Properties.SelectedIndex == 0)
            {

            }
            else if (this.tab_Properties.SelectedIndex == 1)
            { }
            else if (this.tab_Properties.SelectedIndex == 2 && cr[crIndx].im != Rsrc_Images.Type1)
            { }
            else
            { }

            //this.allLanes = cr[index[this.cmbx_crossing.SelectedIndex]].getLaneStatistics();

            //this.pedestrians = cr[crIndx].getPedestrians();
            //if (this.pedestrians != null)
            //{ }
            tab_Properties_Selected(null,null);

        }

        private void tab_Properties_Selected(object sender, TabControlEventArgs e)
        {
            if (tab_Properties.SelectedTab.Text == "Traffic Lights")
                tabPageTLights_Click(null, null);
            else if (tab_Properties.SelectedTab.Text == "Cars")
                tabPageCars_Click(null, null);
            else
                tabPagePedestrians_Click(null, null);
                
        }       
       
        public Map getResults()
        {
            Map mp = new Map();


                for (int i = 0; i < index.Length; i++)
                {
                    mp.AddCrossing(cr[index[i]]);
                }

                return mp;
        }

        private void Edit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (save)
            { DialogResult = DialogResult.OK; }
            else { DialogResult = DialogResult.Cancel; }
        }

        private void tabPageTLights_Click(object sender, EventArgs e)
        {
            
            if (crIndx < 0)
            { return; }
            else
                lsbx_tlights.Items.Clear();

            txb_tl_curInterval.Clear();
            lsbx_tlights.ClearSelected();
            trafficLights = cr[crIndx].getTrafficLights();

            for (int i = 0; i < trafficLights.Length; i++)
            {
                string title = "";
                foreach (TrafficLight tl in trafficLights[i])
                {
                    title += tl.Name + " ";
                }
                lsbx_tlights.Items.Insert(i, title);
            }
        
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txb_tl_NewInterval.Clear();
            lbl_tl_error.Visible = false;
            if (this.lsbx_tlights.SelectedIndex != -1)
                this.txb_tl_curInterval.Text = trafficLights[this.lsbx_tlights.SelectedIndex].ElementAt(0).intervalTobeGreen.ToString();
        }

        private void btn_svTlights_Click(object sender, EventArgs e)
        {
            if (txb_tl_NewInterval.TextLength <= 0)
            {
                lbl_tl_error.Visible = true;
                return;
            }

            int newInterval;

            if (int.TryParse(txb_tl_NewInterval.Text, out newInterval))
            {
                lbl_tl_error.Visible = false;
                foreach (TrafficLight tl in trafficLights[this.lsbx_tlights.SelectedIndex])
                {
                    tl.intervalTobeGreen = newInterval;
                }
                cr[crIndx].getTrafficLights()[this.lsbx_tlights.SelectedIndex] = trafficLights[this.lsbx_tlights.SelectedIndex];
                txb_tl_NewInterval.Clear();
                txb_tl_curInterval.Text = newInterval.ToString();
                save = true;
            }
            else
            {
                lbl_tl_error.Visible = true;
                txb_tl_NewInterval.Clear();
                return;
            }
        }

        private void tabPageCars_Click(object sender, EventArgs e)
        {
            if (crIndx == -1)
                return;
            else
                this.lsbx_car_lanes.Items.Clear();
            this.lsbx_car_lanes.ClearSelected();
            pbx_carImage.Image = null;
            txb_car_curMax.Clear();
            txb_car_curSpeed.Clear();

            allLanes = cr[crIndx].getLanesToEdit();
            int i = 0;
            foreach (Lane l in allLanes)
            {
                this.lsbx_car_lanes.Items.Insert(i,l.LaneId);
                i++;
            }
        }

        private void lsbx_car_lanes_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txb_car_curSpeed.Clear();
            this.pbx_carImage.Image = allLanes[this.lsbx_car_lanes.SelectedIndex].img;

            if (allLanes[this.lsbx_car_lanes.SelectedIndex].speed == -1)
            {
                this.txb_car_curSpeed.Text = "Random";
                this.numUD_car_newSpeed.Value = -1;
            }
            else
            {
                this.txb_car_curSpeed.Text = allLanes[this.lsbx_car_lanes.SelectedIndex].speed.ToString();
                this.numUD_car_newSpeed.Value = allLanes[this.lsbx_car_lanes.SelectedIndex].speed;
            }

            this.txb_car_curMax.Text = allLanes[this.lsbx_car_lanes.SelectedIndex].MAXCARS.ToString();
            this.numUD_car_newMax.Value = allLanes[this.lsbx_car_lanes.SelectedIndex].MAXCARS;
        }

        private void btn_car_save_Click(object sender, EventArgs e)
        {
            if (this.numUD_car_newMax.Value.ToString() == txb_car_curMax.Text && this.numUD_car_newSpeed.Value.ToString() == txb_car_curSpeed.Text)
                return;
            else
                this.allLanes[lsbx_car_lanes.SelectedIndex].MAXCARS = (int)numUD_car_newMax.Value;
                this.allLanes[lsbx_car_lanes.SelectedIndex].speed = (int)numUD_car_newSpeed.Value;
            save = true;

            lsbx_car_lanes_SelectedIndexChanged(null, null);
        }

        private void tabPagePedestrians_Click(object sender, EventArgs e)
        {
            if (crIndx == -1)
                return;
            else
                ls_ped_pedLanes.Items.Clear();
            
            ls_ped_pedLanes.ClearSelected();
            checkBox1.Enabled = false;
            pbx_ped.Image = null;
            numUD_ped_newMax.Enabled = false;

            pedLanes = cr[crIndx].getPedestrianLanes();

            if (pedLanes == null)
            {
                ls_ped_pedLanes.Items.Add("No lanes found");
                return;
            }

            int i =0;
            
            foreach (PedLane p in pedLanes)
            {
                ls_ped_pedLanes.Items.Insert(i, p.LaneId);
                i++;
            }


        }

        private void ls_ped_pedLanes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ls_ped_pedLanes.Items.Count <= 1 || ls_ped_pedLanes.SelectedIndex < 0)
                return;

            this.pbx_ped.Image = pedLanes[ls_ped_pedLanes.SelectedIndex].img;
            checkBox1.Enabled = true;
            this.checkBox1.Checked = pedLanes[ls_ped_pedLanes.SelectedIndex].activeSensor;
            this.txb_ped_curMaxPed.Text = pedLanes[ls_ped_pedLanes.SelectedIndex].MAXCARS.ToString();
            numUD_ped_newMax.Enabled = true;
            this.numUD_ped_newMax.Value = pedLanes[ls_ped_pedLanes.SelectedIndex].MAXCARS;
        }

        private void btn_ped_save_Click(object sender, EventArgs e)
        {
            if (crIndx == -1 || pedLanes == null || ls_ped_pedLanes.SelectedIndex < 0)
                return;

            pedLanes[ls_ped_pedLanes.SelectedIndex].activeSensor = checkBox1.Checked;
            pedLanes[ls_ped_pedLanes.SelectedIndex].MAXCARS = (int)this.numUD_ped_newMax.Value;
            
                save = true;
        }

               
    }
}